package com.wis.bolt.common;

import com.wis.TopologyDrive;
import com.wis.tools.RiskUtiles;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * @Description: traffic_wis
 * @author: fan
 * @Date: Created in 2018/12/17 9:58
 * @Modified By:
 */
public class YaoSuZuHeXianBolt extends BaseRichBolt {
    private OutputCollector collector;
    private static String typeProduce;

    @Override
    public void prepare(Map map, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
        typeProduce = map.get(TopologyDrive.TYPE_PRODUCE).toString();
    }

    @Override
    public void execute(Tuple tuple) {
        String key = tuple.getStringByField("key");
        String value = tuple.getStringByField("value");

        try {
            if (StringUtils.isNotBlank(value)) {
                if (value.equals("over")) {
                    this.collector.emit(new Values(key, "over", "over", "over", "over", "over", "over", "over", "over"));
                } else if (value.equals("jies")) {
                    this.collector.emit(new Values(key, "jies", "jies", "jies", "jies", "jies", "jies", "jies", "jies"));
                } else {
                    String[] split = value.split(",", -1);
                    if (typeProduce.equals("1")) {
                        this.collector.emit(new Values(key, split[0], split[2], split[3], split[5], split[6], split[7], split[8], split[9]));
                    } else if (typeProduce.equals("2")) {
                        // 正常的短临
                        this.collector.emit(new Values(key, split[0],
                                RiskUtiles.foreach(split, 2, 5),
                                RiskUtiles.foreach(split, 6, 9),
                                RiskUtiles.foreach(split, 14, 17),
                                RiskUtiles.foreach(split, 18, 21),
                                RiskUtiles.foreach(split, 22, 25),
                                RiskUtiles.foreach(split, 26, 29),
                                RiskUtiles.foreach(split, 30, 33)
                        ));
                    } else if (typeProduce.equals("3")) {
                        this.collector.emit(new Values(key, split[0],
                                // 能见度
                                RiskUtiles.foreach(split, 2, 74),
                                // 风速
                                RiskUtiles.foreach(split, 75, 147),
                                // 降水量
                                RiskUtiles.foreach(split, 221, 293),
                                // 降水相态
                                RiskUtiles.foreach(split, 294, 366),
                                // 气温
                                RiskUtiles.foreach(split, 367, 439),
                                //相对湿度
                                RiskUtiles.foreach(split, 440, 512),
                                // 道路结冰
                                RiskUtiles.foreach(split, 513, 585)
                        ));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.collector.ack(tuple);
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("key", "id", "nengjiandu", "fengsu", "jiangshuiliang", "jiangshuixiangtai", "qiwen", "xiangduishidu", "daolujiebing"));
    }
}
